var QR = require("../../utils/qrcode.js");
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    imgurl:'../../images/',
    imagePath:'',            //生成二维码的暂存地址
    maskHidden: true,
    placeholder: 'null' ,//默认二维码生成文本
    showIndex:0,
    bridgeList:[],             //处理后得到的桥梁数据
    selectAll:0,
    checkCodeList:[]             //选择要导出的二维码桥梁列表
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //接受送搜索界面拿到的参数
   this.getDealList()
  },
  //判断查询结果是否空
  isEmpty:function(){
   if(this.data.bridgeList.length===0){
    wx.showToast({
      title:'未找到结果',
      icon:'error'
    })
   }
  },
  //获得从search页面处理后的数组
  getDealList:function(){
    var dealList=app.globalData.dealList
    this.setData({
      bridgeList:dealList
    })
    this.isEmpty()
    console.log(this.data.bridgeList) 

  },
  // 全选
  allCheck(e){
    console.log(e)
    var that = this;
    var arr = [];   //存放选中桥梁编号的数组
    for (let i = 0; i < that.data.bridgeList.length; i++) {
      that.data.bridgeList[i].uncheck = (!that.data.selectAll)
    }
    that.setData({
      bridgeList: that.data.bridgeList,
      selectAll: (!that.data.selectAll),
    })
    
  },
  //单选
  checkboxChange: function (e) {
    console.log(e)
    console.log(e.detail.value)
    var index=e.currentTarget.dataset.index
    console.log(index)
    var checkVal=e.detail.value
    if(checkVal=='check'){
      this.data.bridgeList[index].uncheck=1
    }else{
      this.data.bridgeList[index].uncheck=0
    }
    this.setData({
      batchIds: e.detail.value,  //单个选中的值,
    })
    console.log(this.data.bridgeList)
  },

 
  // 获取当前点击索引
  //下拉页面的实现
  panel: function (e) {
    console.log(e)
    if (e.currentTarget.dataset.index != this.data.showIndex) {
      this.setData({
        showIndex: e.currentTarget.dataset.index,
      })
      console.log(this.data.showIndex)
    } else {
      this.setData({
        showIndex: 0
      })
    }
    // var that = this;
    // var codeMsg=this.data.bridgeList[this.data.showIndex-1].bridgeid
    // console.log(this.data.bridgeId)
    // console.log(e)
    // if (codeMsg === "") {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '没有此桥梁信息',
    //     duration: 2000
    //   });
    //   return;
    // }
    // wx.showToast({
    //   title: '加载中...',
    //   icon: 'loading',
    //   duration: 1000
    // });
    // var st = setTimeout(function () {
    //   wx.hideToast()
    //   var size = that.setCanvasSize();
    //   //绘制二维码
    //   that.createQrCode(codeMsg, "mycanvas", size.w, size.h);
    //   clearTimeout(st);
    // }, 2000)
  },
  
  /********************************** 二维码生成过程*/
  // setCanvasSize: function () {
  //   var size = {};
  //   try {
  //     var res = wx.getSystemInfoSync();
  //     var scale = 750 / 686; //不同屏幕下canvas的适配比例；设计稿是750宽
  //     var width = res.windowWidth / scale;
  //     var height = width; //canvas画布为正方形
  //     size.w = width;
  //     size.h = height;
  //   } catch (e) {
  //     // Do something when catch error
  //     console.log("获取设备信息失败" + e);
  //   }
  //   return size;
  // },
  // createQrCode: function (url, canvasId, cavW, cavH) {
  //   //调用插件中的draw方法，绘制二维码图片
  //   QR.api.draw(url, canvasId, cavW, cavH, this, this.canvasToTempImage);
  //   // setTimeout(() => { this.canvasToTempImage();},100);

  // },
  // //获取临时缓存照片路径，存入data中
  // canvasToTempImage: function () {
  //   var that = this;
  //   wx.canvasToTempFilePath({
  //     canvasId: 'mycanvas',
  //     success: function (res) {
  //       var tempFilePath = res.tempFilePath;
  //       console.log(tempFilePath);
  //       that.setData({
  //         imagePath: tempFilePath,
  //       });
  //       console.log(that.data.imagePath)
  //     },
  //     fail: function (res) {
  //       console.log(res);
  //     }
  //   }, that);
  // },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var index=e.currentTarget.dataset.index
    var img = this.data.bridgeList[index].codeimage;
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
/******************************* */
//将生成的二维码存到imageList中

//返回搜索页面
  reback:function(){
    this.setData({
      bridgeList:[]
    })
    wx.reLaunch({
      url:'/pages/search/index',
  })
  
  },
  //保存图片到本地
  saveImg:function(e){
  let that = this;
  //获取相册授权
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.writePhotosAlbum']) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            //这里是用户同意授权后的回调
            that.saveImgToLocal();
          },
          fail() {//这里是用户拒绝授权后的回调
            that.setData({
              openSettingBtnHidden: false
            })
          }
        })
      } else {//用户已经授权过了
        that.saveImgToLocal();
      }
    }
  })

  },
  //单个导出
  saveImgToLocal: function (e) {
    console.log(e)
    var index=e.currentTarget.dataset.index
    let that = this;
    let imgSrc = that.data.bridgeList[index].codeimage;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
      //图片保存到本地
      wx.saveImageToPhotosAlbum({
        filePath: res.tempFilePath,
        success: function (data) {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        },
      })
    },
    fail:function(res){
      console.log(res)
    }
  })
  },
//批量导出
  saveMuchImgToLocal:function(e){
    for(var index in this.data.bridgeList){
      console.log(this.data.bridgeList[index].uncheck)
      if(this.data.bridgeList[index].uncheck==1){
        let imgSrc=this.data.bridgeList[index].codeimage
        wx.downloadFile({
          url: imgSrc,
          success: function (res) {
            console.log(res);
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (data) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
          })
        },
        fail:function(res){
          console.log(res)
        }
      })
      }
    }

  },
// 授权
  handleSetting: function (e) {
  let that = this;
  // 对用户的设置进行判断，如果没有授权，即使用户返回到保存页面，显示的也是“去授权”按钮；同意授权之后才显示保存按钮

  if (!e.detail.authSetting['scope.writePhotosAlbum']) {
    // wx.showModal({
    //   title: '警告',
    //   content: '若不打开授权，则无法将图片保存在相册中！',
    //   showCancel: false
    // })
    that.setData({
      openSettingBtnHidden: false
    })
  } else {
    // wx.showModal({
    //   title: '提示',
    //   content: '您已授权，赶紧将图片保存在相册中吧！',
    //   showCancel: false
    // })
    that.setData({
      openSettingBtnHidden: true
    })
  }
  },
  
})